package com.luge.composedemo

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyGridItemSpanScope
import androidx.compose.foundation.lazy.grid.LazyGridScope
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.layout
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
@Preview
@Composable
fun MyBasicColumn(){
//    var info  by remember {
//        mutableStateOf("")
//    }
    Column(
//        modifier = Modifier
//            .width(300.dp)
//            .height(400.dp)
//            .background(Color.Blue),
//        horizontalAlignment = Alignment.CenterHorizontally,
//        verticalArrangement = Arrangement.SpaceAround
    ) {
        Button(onClick = {
            print("点击我!!");
        }) {
            Text(text = "点击我")
        }
//        if(info.isEmpty())
//        Text(text = "测试文本")
//        OutlinedTextField(value = info, onValueChange = {
//            info = it
//        })
        Row(){
            Button(onClick = {
                print("点击我!!");
            }) {
                Text(text = "点击我")
            }
        }
    }
//
//
}
//
//@Preview
//@Composable
//// framelayout
//fun BoxLayout(){
//    Box(modifier = Modifier
//        .fillMaxSize()
//        .padding(10.dp)) {
//        Button(onClick = {
//            print("点击我!!");
//        }, modifier = Modifier.align(Alignment.TopStart)) {
//            Text(text = "点击我")
//        }
//
//        Button(onClick = {
//            print("点击我!!");
//        }, modifier = Modifier.align(Alignment.BottomCenter)) {
//            Text(text = "点击我")
//        }
//    }
//}
//
//@Preview
//@Composable
//fun FrameLayout(){
//    Box(modifier = Modifier.fillMaxSize()) {
//        Image(painter = painterResource(id = R.mipmap.desktop), contentDescription = "")
//        Text(text = "hello java", color = Color.Red, modifier = Modifier.padding(30.dp))
//    }
//}
//
//
//@Preview
//@Composable
//fun GridViewLayout(){
//    LazyVerticalGrid(columns = GridCells.Fixed(2),modifier = Modifier.padding(8.dp,20.dp)){
//
//        item() {
//            Image(painter = painterResource(id = R.mipmap.desktop), contentDescription = "", modifier = Modifier.padding(10.dp))
//        }
//        item {
//            Image(painter = painterResource(id = R.mipmap.desktop), contentDescription = "")
//        }
//        item {
//            Image(painter = painterResource(id = R.mipmap.desktop), contentDescription = "")
//        }
//        item {
//            Image(painter = painterResource(id = R.mipmap.desktop), contentDescription = "")
//        }
//    }
//}
//
//@Preview
//@Composable
//fun MyButton(){
//    Button(onClick = { /*TODO*/ }, contentPadding = PaddingValues(
//        start = 20.dp,
//        top = 12.dp,
//        end = 20.dp,
//        bottom = 20.dp,
//    )
//    ) {
//        Icon(
//            Icons.Filled.Favorite,
//            contentDescription = "",
//            modifier = Modifier.size(ButtonDefaults.IconSize)
//        )
//        Spacer(Modifier.size(ButtonDefaults.IconSpacing))
//        Text("Like")
//    }
////    ExtendedFloatingActionButton(onClick = { /*TODO*/ },
////        icon = {
////            Icon( Icons.Filled.Favorite,
////            contentDescription = "",
////            modifier = Modifier.size(ButtonDefaults.IconSize))
////        }, text = {
////            Text("like")
////        })
//
//}
//
//@Preview
//@Composable
//fun MyImage(){
//    Image(painter = painterResource(id = R.mipmap.desktop), contentDescription = "",
//        modifier = Modifier
//            .padding(top = 10.dp)
//            .size(40.dp)
//            .clip(CircleShape)
//            .border(
//                1.dp,
//                MaterialTheme.colorScheme.onSecondaryContainer,
//                CircleShape
//            )
//            .fillMaxSize())
//
//}
//


//@Preview
//@Composable
//fun test(){
//    val scope = rememberCoroutineScope()
//    Scaffold(
//        topBar = {
//            TopAppBar(title = { /*TODO*/ })
//        },
//        bottomBar = {
//            BottomAppBar {
//
//            }
//        },
//        floatingActionButton = {
//            ExtendedFloatingActionButton(
//                text = { /*TODO*/ },
//                icon = { /*TODO*/ },
//                onClick = {
//                    scope.launch {
//                        //tood
//                    }
//                })
//        }
//    ){
//        content ->
//        Box(modifier = Modifier.padding(content)){
//            Text(text = "ceshi")
//        }
//    }
//}

@Composable
fun MyBasicColumn(modifier: Modifier = Modifier,
                  content:@Composable ()->Unit) {
    Layout(modifier = modifier, content = content) { measureable, constrians ->
        val result = measureable.map { measureable ->
            measureable.measure(constrians)
        }
        layout(
            constrians.maxWidth, constrians.maxHeight
        ) {
            var y = 0
            result.forEach { placeable ->
                placeable.placeRelative(x = 0, y = y)
                y += placeable.height
            }
        }
    }
}



////自定义布局
//fun Modifier.customerLayout() = this.layout { measureable, constrians ->
//    ...
//}

